﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;

namespace Gurucore.Framework.DataAccess.Persistence
{
	public class ComplexTypeInfoManager
	{
		private Dictionary<Type, ComplexTypeInfo> m_infos;

		public ComplexTypeInfoManager()
		{
			m_infos = new Dictionary<Type, ComplexTypeInfo>();
		}

		public bool IsComplexType(Type p_oType)
		{
			return (p_oType.GetCustomAttributes(typeof(MappedTableAttribute), true).Length == 0);
		}

		public ComplexTypeInfo GetComplexTypeInfo(Type p_oType)
		{
			if (m_infos.ContainsKey(p_oType))
			{
				return m_infos[p_oType];
			}
			else
			{
				lock (this)
				{
					ComplexTypeInfo info = new ComplexTypeInfo();
					PropertyInfo[] props = p_oType.GetProperties();
					foreach (PropertyInfo prop in props)
					{
						if (prop.PropertyType.GetCustomAttributes(typeof(MappedTableAttribute), true).Length > 0)
						{
							info.AddElement(prop);
						}
					}
					m_infos.Add(p_oType, info);

					return info;
				}
			}
		}
	}
}
